package chpt09.testsync;

import java.util.Scanner;

class SimpleTask implements Runnable{
    private volatile boolean stop = false;
    public void stop(){
        stop = true;
    }
    public boolean getStop() {
    	return stop;
    }
    @Override
    public void run() {
        while(!getStop()){
            // do sth 
        }
    }
}

public class VolatileTest{
	public static void main(String[] args) {
		String name = Thread.currentThread().getName();
    	System.out.println(name+" start to run!");
    	System.out.println(name+" end!");
    	
		SimpleTask st = new SimpleTask();
		Thread t1 = new Thread(st);
		t1.start();
		System.out.println("是否要停止t1线程?(y/n)");
		Scanner sc = new Scanner(System.in);
		if ("y".equalsIgnoreCase(sc.next())) {
			st.stop();
		}else {
			System.out.println("不停止!");
		}
		System.out.println("main end");
		sc.close();
	}
}

